package cn.jietuo.juc.c_010_interview3;

import java.util.concurrent.CountDownLatch;

/**
 * @author zhangx & jietuo_zx@163.com
 * @version 1.0
 * @date 2020/5/13
 * @description:
 */
public class T03_Sync_Wait_Notify {

    static char[] str1 = new char[]{'1', '2', '3', '4', '5', '6', '7', '8'};
    static char[] str2 = new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'};

    static CountDownLatch countDownLatch = new CountDownLatch(1);

    public static void main(String[] args) {
        Object o = new Object();
        Thread t1 = new Thread(() -> {
            synchronized (o){
                    for (int i = 0; i < str1.length; i++) {
                        System.out.println(str1[i]);
                        countDownLatch.countDown();
                        o.notify();
                        try {
                            o.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    o.notify();
            }
        },"t1");

        Thread t2 = new Thread(() -> {
            try {
                countDownLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (o){
                for (int i = 0; i < str2.length; i++) {
                    try {

                        System.out.println(str2[i]);
                        o.notify();
                        o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                o.notify();
            }
        },"t2");

        t2.start();
        t1.start();
    }
}
